package city.spring.modules.oldman.controller;

import city.spring.modules.oldman.entity.OldmanFixedcostsEntity;
import city.spring.modules.oldman.service.OldmanFixedcostsService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：老人入院固定缴费
 *
 * @author 谢金婷
 * @date 2020-08-19 16:32:49
 */
@RestController
@RequestMapping("/oldman_fixedcosts")
public class OldmanFixedcostsController extends ApiController {
    private final OldmanFixedcostsService oldmanFixedcostsService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<OldmanFixedcostsEntity, ?>> entityOrderFields;

    public OldmanFixedcostsController(OldmanFixedcostsService oldmanFixedcostsService) {
        this.oldmanFixedcostsService = oldmanFixedcostsService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(OldmanFixedcostsEntity::getId);
        entityOrderFields.add(OldmanFixedcostsEntity::getOldmanId);
        entityOrderFields.add(OldmanFixedcostsEntity::getItemId);
        entityOrderFields.add(OldmanFixedcostsEntity::getFee);
        entityOrderFields.add(OldmanFixedcostsEntity::getRemark);
    }

    /**
     * 获取全部的 <strong>老人入院固定缴费</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault Pageable pageable, OldmanFixedcostsEntity entity) {
        LambdaQueryChainWrapper<OldmanFixedcostsEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>老人入院固定缴费</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault Pageable pageable, OldmanFixedcostsEntity entity) {
        LambdaQueryChainWrapper<OldmanFixedcostsEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<OldmanFixedcostsEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        return success(page);
    }

    /**
     * 获取一个 <strong>老人入院固定缴费</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(oldmanFixedcostsService.getById(id));
    }

    /**
     * 添加一个 <strong>老人入院固定缴费</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody OldmanFixedcostsEntity entity) {
        oldmanFixedcostsService.saveOldmanFixedcosts(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>老人入院固定缴费</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody OldmanFixedcostsEntity entity) {
        entity.setId(id);
        oldmanFixedcostsService.updateOldmanFixedcosts(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>老人入院固定缴费</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        oldmanFixedcostsService.deleteOldmanFixedcosts(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>老人入院固定缴费</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        oldmanFixedcostsService.deleteOldmanFixedcosts(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<OldmanFixedcostsEntity> buildLambdaQuery(OldmanFixedcostsEntity entity) {
        LambdaQueryChainWrapper<OldmanFixedcostsEntity> lambdaQuery = oldmanFixedcostsService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), OldmanFixedcostsEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getOldmanId()), OldmanFixedcostsEntity::getOldmanId, entity.getOldmanId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getItemId()), OldmanFixedcostsEntity::getItemId, entity.getItemId());
        lambdaQuery.eq(entity.getFee() != null, OldmanFixedcostsEntity::getFee, entity.getFee());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getRemark()), OldmanFixedcostsEntity::getRemark, entity.getRemark());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<OldmanFixedcostsEntity> buildQuery(OldmanFixedcostsEntity entity) {
        QueryWrapper<OldmanFixedcostsEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(entity.getId()), "id", entity.getId());
        wrapper.like(StringUtils.isNotBlank(entity.getOldmanId()), "oldman_id", entity.getOldmanId());
        wrapper.like(StringUtils.isNotBlank(entity.getItemId()), "item_id", entity.getItemId());
        wrapper.eq(entity.getFee() != null, "fee", entity.getFee());
        wrapper.like(StringUtils.isNotBlank(entity.getRemark()), "remark", entity.getRemark());
        return wrapper;
    }

    /**
     * 根据老人id查找最新的入院固定费用
     *
     * @param oldmanId
     * @return
     */
    @GetMapping("getFixedcostsByOldmanId/{oldmanId}")
    public Object getFixedcostsByOldmanId(@PathVariable String oldmanId) {
        return success(oldmanFixedcostsService.getFixedcostsByOldmanId(oldmanId));
    }

}
